#! /bin/bash
cpath=$(cd `dirname $0`; pwd)
 
BinFile="$1"
BinShell="$2"
 
if [ "A$#" != "A2" ];then
    echo -e "\n\n===> The Script $0 need two parameters.\n"
    echo -e "\t\$1: Binary filename."
    echo -e "\t\$2: output script name."
    echo -e "\n\n"
    exit 200
fi
 
cd ${cpath};
cat > ${cpath}/${BinShell} << "EOOF"
#! /bin/bash
 
cpath=$(cd `dirname $0`; pwd)
 
PAYLOAD_LINE=$(awk '/^__BIN_BEGINS__/ { print NR + 1; exit 0; }' $0)
PAYLOAD_END=$(awk '/^__BIN_END__/ { print NR - 1; exit 0; }' $0)
 
head -n ${PAYLOAD_END} $0 | tail -n +${PAYLOAD_LINE} | base64 -d &> ${cpath}/__MEISBINFILENAME__
echo -e "\n\n======================================================\n\n"
echo -e "\t Binary File: __MEISBINFILENAME__ is export successed."
echo -e "\t export path: ${cpath}/__MEISBINFILENAME__"
echo -e "\n\n======================================================\n\n"

#tar xvf dpkg.txz
#chmod +x install.sh
#./install.sh
#rm -fr dpkg.txz

EOOF
 
cat >> ${cpath}/${BinShell} << EOF
function BASE64
{
__BIN_BEGINS__
`base64 ${cpath}/${BinFile}`
__BIN_END__
}
EOF
 
# replace  __MEISBINFILE__
sed -i "s#__MEISBINFILE__#${cpath}/${BinFile}#g" ${cpath}/${BinShell}
sed -i "s#__MEISBINFILENAME__#${BinFile}#g" ${cpath}/${BinShell}